/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vista;

import control.Control;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import modelo.Jugador;
import persistencia.PersiJugador;

/**
 *
 * @author alumneDAM
 */
public class PantallaRegistro extends javax.swing.JFrame {

    /**
     * Creates new form NewJFrame
     */
    Control control;

    public PantallaRegistro(Control control) {
        initComponents();

        this.control = control;
        this.btn_registrar.addActionListener(control);

        this.setLocationRelativeTo(null);
        this.setIconImage(new ImageIcon(getClass().getResource("../imagenes/logo_pequeno.png")).getImage());
    }

    public boolean compruebaRegistro() {
        boolean correcto = true;
        String salida = "";
        PersiJugador pj = new PersiJugador();
        Jugador jugador = null;
        String nickname = tfNickname.getText();
        String nombre = tfNombre.getText();
        String pass = pfPassword.getText();
        String pass1 = pfPasswordr.getText();

        jugador = pj.leerJugador(nickname);
        if (jugador != null) {
            salida = "Jugador ya existe";
        }
        if (!pass.equals(pass1)) {
            System.out.println("pass " + pass);
            salida = salida + " Contraseñas son diferentes " + pass + " " + pass1;
        }
        if (nombre.isEmpty()) {
            salida = salida + " El nombre esta vacio";
        }
        if (!salida.isEmpty()) {
            JOptionPane.showMessageDialog(rootPane, salida, "Error", JOptionPane.INFORMATION_MESSAGE);
            correcto = false;
        }
        return correcto;
    }

    public Jugador nuevoJugador() {

        return new Jugador(tfNickname.getText(), tfNombre.getText(), pfPassword.getText());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pfPassword = new javax.swing.JPasswordField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        btn_registrar = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        pfPasswordr = new javax.swing.JPasswordField();
        jLabel4 = new javax.swing.JLabel();
        tfNombre = new javax.swing.JTextField();
        tfNickname = new javax.swing.JTextField();
        lbl_logo = new javax.swing.JLabel();

        setTitle("REGISTRO USUARIO");
        setResizable(false);

        jLabel1.setText("Nombre");

        jLabel2.setText("Contraseña");

        btn_registrar.setText("Registrar");

        jLabel3.setText("Nickname");

        pfPasswordr.setToolTipText("");

        jLabel4.setText("Repetir contraseña");

        lbl_logo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/logo.jpg"))); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(209, Short.MAX_VALUE)
                .addComponent(btn_registrar)
                .addGap(166, 166, 166))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(98, 98, 98)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel4)
                                .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING))
                            .addComponent(jLabel3)
                            .addComponent(jLabel1))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(pfPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)
                            .addComponent(pfPasswordr, javax.swing.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)
                            .addComponent(tfNickname)
                            .addComponent(tfNombre)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(164, 164, 164)
                        .addComponent(lbl_logo)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(39, 39, 39)
                .addComponent(lbl_logo)
                .addGap(42, 42, 42)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tfNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(tfNickname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(pfPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pfPasswordr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btn_registrar)
                .addGap(41, 41, 41))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_registrar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel lbl_logo;
    private javax.swing.JPasswordField pfPassword;
    private javax.swing.JPasswordField pfPasswordr;
    private javax.swing.JTextField tfNickname;
    private javax.swing.JTextField tfNombre;
    // End of variables declaration//GEN-END:variables
}
